import logging
from django_redis import get_redis_connection
from redis import RedisError
from rest_framework import serializers

logger = logging.getLogger('doudou')


class RegisterSMSCodeSerializer(serializers.Serializer):
    text = serializers.CharField(label='用户输入的验证码', max_length=4, min_length=4, required=True)
    image_code_id = serializers.UUIDField(label='验证码唯一性id')

    def validate(self, attrs):
        # 获取用户提交的验证码
        text = attrs['text']
        image_code_id = attrs['image_code_id']
        # 链接redis 获取redis中的验证码
        redis_conn = get_redis_connection('code')
        redis_text = redis_conn.get('img_%s' % image_code_id)

        # 校验验证码是否过期
        if redis_text is None:
            raise serializers.ValidationError('验证码已过期')

        # 将验证码从redis中删除
        try:
            redis_conn.delete('img_%s' % image_code_id)
        except RedisError as e:
            # 记录日志
            logger.error(e)
        # 对redis的验证码编码之后进行比对,要注意大小写问题
        if redis_text.decode().lower() != text.lower():
            raise serializers.ValidationError('验证码输入有误')
        return attrs
